const blogTextSer = require('../../service/blog/blogTextSer')
const saveImg = require('../../utils/saveImg')
const saveVideo = require("../../utils/saveVideo");

class blogTextCon {

    // 查询博客列表
    async getBlogTextList(ctx) {
        const searchData = ctx.request.body.searchData || null
        const listQuery = ctx.request.body.listQuery
        try {
            const data = await blogTextSer.getBlogTextList(searchData, listQuery)
            ctx.body = {
                code: '200',
                data: data,
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


    // 查询博客内容
    async getBlogTextOne(ctx) {
        const searchData = ctx.request.body
        try {
            const data = await blogTextSer.getBlogTextOne(searchData)
            ctx.body = {
                code: '200',
                data: data,
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


    // 添加博客
    async addBlogText(ctx) {
        const jsonData = ctx.request.body
        //添加进数据库的数据
        const addData = {
            blogCoverTitle: jsonData.blogCoverTitle,
            blogTypeIdK: jsonData.blogTypeId,
            blogTypeNameK: jsonData.blogTypeName,
            blog_content: jsonData.htmlText,
            blogTitle: jsonData.blogTitle,
            blogUserId: jsonData.blogUserId,
            blog_cover_image: jsonData.blogCoverImage,
            blogStatus: 0,
        }
        try {
            const data = await blogTextSer.addBlogText(addData)
            if (data.dataValues.id) {
                ctx.body = {
                    code: '200',
                    message: '添加成功'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '添加失败'
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


    // 删除博客
    async deleteBlog(ctx) {
        const reqData = {id: ctx.request.body.id}
        try {
            const data = await blogTextSer.deleteBlog(reqData)
            if (data === 1) {
                ctx.body = {
                    code: '200',
                    message: '删除成功'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '删除失败'
                }
            }

        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)

        }
    }

    // 修改
    async editBlog(ctx) {
        const where = ctx.request.body.editData  // 编辑的条件
        const reqData = ctx.request.body.reqData // 修改的字段数据
        try {
            const data = await blogTextSer.editBlog(where, reqData)
            if (data[0] === 1) {
                ctx.body = {
                    code: '200',
                    message: '编辑成功'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '编辑失败'
                }
            }

        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


    // 修改博客内容
    async editBlogText(ctx) {
        const jsonData = ctx.request.body
        const where = {id: jsonData.editId}
        const reqData = {
            blogCoverTitle: jsonData.blogCoverTitle,
            blogCoverImage: jsonData.blogCoverImage,
            blogTypeIdK: jsonData.blogTypeIdK,
            blogTypeNameK: jsonData.blogTypeNameK,
            blog_content: jsonData.htmlText,
            blogTitle: jsonData.blogTitle,
            blogUserId: jsonData.blogUserId,
        }
        try {
            const data = await blogTextSer.editBlog(where, reqData)
            if (data[0] === 1) {
                ctx.body = {
                    code: '200',
                    message: '编辑成功'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '编辑失败'
                }
            }

        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


    // 添加博客图片
    async addBlogImg(ctx) {
        try {
            const blogUserId = ctx.request.body.blogUserId || ''
            const imgFile = ctx.request.files.file
            // 保存图片
            let ImgUrl
            try {
                ImgUrl = saveImg(imgFile, blogUserId)
            } catch (e) {
                ctx.app.emit('error', e.toString(), ctx)

            }
            ctx.body = {
                errno: 0, // 注意：值是数字，不能是字符串
                data: {
                    url: ImgUrl,
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    // 添加博客视频
    async addBlogVideo(ctx) {
        try {
            const blogUserId = ctx.request.body.blogUserId
            const videoFile = ctx.request.files.file
            // 保存图片
            const videoUrl = await saveVideo(videoFile, blogUserId).catch(e => {
                console.log(e)
            })
            ctx.body = {
                errno: 0, // 注意：值是数字，不能是字符串
                data: {
                    url: videoUrl.split('public')[1].replace(/\\/g, '/'), // 图片 src ，必须
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

    // 修改博客的浏览次数
    async setViewCount(ctx) {
        const blogId = ctx.request.body.blogId
        const setType = ctx.request.body.setType
        if (!blogId) {
            return ctx.body = {
                code: '200',
                message: '博客id不能为空'
            }
        }
        try {
            const searchData = await blogTextSer.searchViewAndComment(blogId)
            let data = null
            if (setType === '1') {
                const blogPageView = searchData[0].dataValues.blogPageView
                data = await blogTextSer.editBlog({id: blogId}, {blogPageView: blogPageView + 1})
            } else {
                const blogCommentNumber = searchData[0].dataValues.blogCommentNumber
                data = await blogTextSer.editBlog({id: blogId}, {blogCommentNumber: blogCommentNumber + 1})
            }
            if (data[0]) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.body = {
                    code: '999'
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

}


module.exports = new blogTextCon()
